package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.DetailReceipt;
import com.ruoyi.common.core.domain.entity.HeadReceipt;
import com.ruoyi.common.core.domain.entity.InventoryProduct;
import com.ruoyi.common.core.domain.entity.ReceiptFrom;
import com.ruoyi.system.mapper.DetailReceiptMapper;
import com.ruoyi.system.mapper.HeadReceiptMapper;
import com.ruoyi.system.mapper.InventoryMapper;
import com.ruoyi.system.service.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Validator;
import java.util.List;

/**
 * 库存单据 业务层处理
 *
 * @author KC
 */
@Service
public class InventoryServiceImpl implements InventoryService {

    @Autowired
    protected Validator validator;
    @Autowired
    private HeadReceiptMapper HRMapper;
    @Autowired
    private DetailReceiptMapper DRMapper;
    @Autowired
    private InventoryMapper ItMapper;

    /**
     * 库存货品查询
     */
    @Override
    public List<InventoryProduct> selectInventoryProduct(InventoryProduct bo) {
        return ItMapper.selectInventoryProduct(bo);
    }


    /**
     * 库存单据头表查询
     */
    @Override
    public List<HeadReceipt> headReceiptQuery(HeadReceipt bo) {
        return HRMapper.headReceiptQuery(bo);
    }

    /**
     * 库存单据明细查询
     */
    @Override
    public List<DetailReceipt> detailReceiptQuery(DetailReceipt bo) {
        return DRMapper.SelectDetailReceiptQuery(bo);
    }

    /**
     * 货品发生汇总查询
     */
    @Override
    public List<DetailReceipt> inventorySummaryQuery(DetailReceipt bo) {
        return DRMapper.inventorySummaryQuery(bo);
    }

    /**
     * 库存指定单据查询
     */
    @Override
    public HeadReceipt selectHeadReceiptById(String SystematicReceipt) {
        return HRMapper.selectHeadReceiptById(SystematicReceipt);
    }

    /**
     * 新增入库单据明细
     */
    private void addDetails(List<DetailReceipt> details) {
        long count = ItMapper.selectInventoryById(details).size();
        DRMapper.addDetailReceipt(details);
        if (count == 0) {
            ItMapper.addInventory(details);
        } else {
            ItMapper.updateWarehousingInventory(details);
        }
    }

    /**
     * 新增出库单据明细
     */
    private void outboundDetails(List<DetailReceipt> details) {
        long count = ItMapper.selectInventoryById(details).size();
        if (count == 0) {
            throw new RuntimeException("货品明细中有货品库存为0，货品需入库后才能出库！");
        } else {
            DRMapper.addDetailReceipt(details);
            ItMapper.updateRetrievalInventory(details);
        }
    }

    /**
     * 修改库存单据明细
     */
    private void updateDetails(List<DetailReceipt> details) {
        DRMapper.delDetailReceipt(details);
        DRMapper.addDetailReceipt(details);
        ItMapper.updateWarehousingInventory(details);
        ItMapper.updateRetrievalInventory(details);
    }


    /**
     * 库存调拨单据明细
     */
    private void transferDetails(List<DetailReceipt> details) {
        long count = ItMapper.selectInventoryById(details).size();
        DRMapper.addDetailReceipt(details);
        if (count == 0) {
            ItMapper.addInventory(details);
        } else {
            ItMapper.updateWarehousingInventory(details);
            ItMapper.updateRetrievalInventory(details);
        }
    }

    /**
     * 保存库存单据信息
     */
    @Override
    public int saveInventoryReceipt(ReceiptFrom bo) {
        long count = HRMapper.selectHeadReceiptByCount(bo).size();
        List<DetailReceipt> details = bo.getDetails();
        if (count == 0) {
            if (bo.getReceiptType() == 5 || bo.getReceiptType() == 8) {
                addDetails(details);
            } else if (bo.getReceiptType() == 6) {
                outboundDetails(details);
            } else if (bo.getReceiptType() == 7) {
                transferDetails(details);
            } else {
                throw new RuntimeException("单据类型不存在，请确认！");
            }
            return HRMapper.addHeadReceipt(bo);
        } else {
            updateDetails(details);
            return HRMapper.updateHeadReceipt(bo);
        }
    }

    /**
     * 修改库存单据信息
     */
    @Override
    public int updateInventoryReceipt(ReceiptFrom bo) {
        List<DetailReceipt> details = bo.getDetails();
        updateDetails(details);
        return HRMapper.updateHeadReceipt(bo);
    }


    /**
     * 删除库存单据信息
     */
    @Override
    @Transactional
    public int delInventoryReceipt(List<DetailReceipt> bo) {
        /*     删除单据明细      */
        DRMapper.delDetailReceipt(bo);
        /*     入库货品修改      */
        ItMapper.updateWarehousingInventory(bo);
        /*     出库货品修改      */
        ItMapper.updateRetrievalInventory(bo);
        /*     删除单据头表      */
        return HRMapper.delHeadReceipt(bo);
    }

}
